<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="450" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="所属字典" prop="dictTypeId">
				<el-select v-model="form.dictTypeId" style="width: 100%;" clearable disabled>
					<el-option v-for="(item, index) in dict" :key="index" :label="item.dictName" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="字典项" prop="dictItem">
				<el-input v-model="form.dictItem" clearable></el-input>
			</el-form-item>
			<el-form-item label="字典项值" prop="dictValue">
				<el-input v-model="form.dictValue" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序号" prop="orderNum">
				<el-input-number v-model="form.orderNum" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" type="textarea" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			// 表单元素控制
			mode: "add",
			titleMap: {
				add: '新增字典项',
				edit: '编辑字典项'
			},
			visible: false,
			isSaving: false,
			// 所需数据选项
			dict: [],
			dictProps: {
				value: "id",
				label: "dictName",
				emitPath: false,
				checkStrictly: true
			},
			// 表单数据
			form: {
				id: null,
				dictTypeId: null,
				dictItem: null,
				dictValue: null,
				orderNum: 1,
				status: "1",
				remark: null
			},
			// 表单验证规则
			rules: {
				dictTypeId: [
					{required: true, message: '请选择所属字典'}
				],
				dictItem: [
					{required: true, message: '请输入字典项'}
				],
				dictValue: [
					{required: true, message: '请输入字典项值'}
				],
				orderNum: [
					{required: true, message: '请输入排序号'}
				],
				status: [
					{required: true, message: '请选择状态'}
				]
			}
		}
	},
	mounted() {
		if (this.params) {
			this.form.dictTypeId = this.params.id;
		}
		this.getDict();
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		// 获取字典列表
		async getDict() {
			this.dict = await this.$API.system.dict.list.get();
		},
		// 表单注入数据
		setData(data) {
			console.log(data)
			this.form.id = data.id;
			this.form.dictTypeId = data.dictTypeId;
			this.form.dictItem = data.dictItem;
			this.form.dictValue = data.dictValue;
			this.form.orderNum = data.orderNum ? data.orderNum : 1;
			this.form.status = data.status ? data.status : '1';
			this.form.remark = data.remark;
		},
		// 表单提交方法
		submit() {
			this.$refs.dialogForm.validate((valid) => {
				if (valid) {
					this.isSaving = true;
					if (this.form.id) {
						this.$API.system.dictData.edit.put(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					} else {
						this.$API.system.dictData.add.post(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					}
				}
			})
		}
	}
}
</script>

<style>
</style>
